package com.cu.current;

import java.util.concurrent.locks.ReentrantReadWriteLock;

public class CopyAndWriteLockDemo {

    private static ReentrantReadWriteLock lock = new ReentrantReadWriteLock();


    public static void main(String[] args) {

        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    lock.readLock().lock();
                    System.out.println("thread1 read");
                    String a = "read result";
                    if(a != null){
                        lock.writeLock().lock();
                        System.out.println("thread1 write");
                    }
                }finally {
                    lock.readLock().unlock();
                    lock.writeLock().unlock();
                }
            }
        });

        Thread thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    lock.readLock().lock();
                    System.out.println("thread1 read");
                    String a = "read result";
                    if(a != null){
                        lock.writeLock().lock();
                        System.out.println("thread1 write");
                    }
                }finally {
                    lock.readLock().unlock();
                    lock.writeLock().unlock();
                }
            }
        });

        thread1.start();
        thread2.start();

    }
}
